package com.yang.drawcard.base;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

import com.blankj.utilcode.util.FileUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.target.Target;
import com.core.app.Latte;
import com.core.initbase.InitFragment;
import com.core.utils.dbtutil.DbtLog;
import com.core.utils.exit.ExitAppUtils;
import com.core.utils.file.FileTool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * Created by ywm on 2018/8/23.
 */

public class BaseFragment extends InitFragment {


    // Android 7.0调用系统相机适配 使用FileUtils库中的方法转化
    public Intent toCameraByContentResolver(Intent intent, File tempFile, String currentPhotoName) {
        final ContentValues contentValues = new ContentValues(1);// ?
        // contentValues.put(MediaStore.Images.Media.DATA, tempFile.getPath());// ?
        String path1 = tempFile.getPath();
        String path2 = tempFile.getAbsolutePath();
        DbtLog.logUtils("BaseFragmentpath1",  path1);
        DbtLog.logUtils("BaseFragmentpath2",  path2);
        contentValues.put(MediaStore.Images.Media.DATA, tempFile.getAbsolutePath());// ?
        // Uri uri = getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
        Uri uri = getImageContentUri(tempFile);

        DbtLog.logUtils("BaseFragment",  uri.toString());

        // 需要将Uri路径转化为实际路径?
        final File realFile = FileUtils.getFileByPath(FileTool.getRealFilePath(getContext(), uri));
        // 将File转为Uri
        final Uri realUri = Uri.fromFile(realFile);


        //限制只调用系统相机
        final Intent intent_camera = ((Activity)getContext()).getPackageManager().getLaunchIntentForPackage("com.android.camera");
        if (intent_camera != null) {
            intent.setPackage("com.android.camera");
        }
        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);

        // intent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_FRONT);//前置摄像头
        intent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_BACK);//后置摄像头


        //将拍取的照片保存到指定URI
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        CameraImageBean cameraImageBean = CameraImageBean.getInstance();
        cameraImageBean.setmPath(realUri);
        cameraImageBean.setPicname(currentPhotoName);

        return intent;
    }



    /**

     * 转换 content:// uri

     *

     * @param imageFile

     * @return

     */

    public Uri getImageContentUri(File imageFile) {

        String filePath = imageFile.getAbsolutePath();

        Cursor cursor = getContext().getContentResolver().query(

                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

                new String[]{MediaStore.Images.Media._ID},

                MediaStore.Images.Media.DATA + "=? ",

                new String[]{filePath}, null);

        if (cursor != null && cursor.moveToFirst()) {

            @SuppressLint("Range")
            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));

            Uri baseUri = Uri.parse("content://media/external/images/media");

            Uri uri= Uri.withAppendedPath(baseUri, "" + id);

            DbtLog.logUtils("BaseFragmentgetImageContentUri1",  uri.toString());

            return uri;

        } else {

            /*if (imageFile.exists()) {

                ContentValues values = new ContentValues();

                values.put(MediaStore.Images.Media.DATA, filePath);

                return getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            } else {

                return null;

            }*/

            ContentValues values = new ContentValues();

            values.put(MediaStore.Images.Media.DATA, filePath);

            Uri uri= getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            DbtLog.logUtils("BaseFragmentgetImageContentUri2",  uri.toString());

            return uri;

        }

    }

    /**
     * 通过uri获取图片并进行压缩
     *
     * @param uri
     */
    public static Bitmap getBitmapFormUri(Context ac, Uri uri) throws FileNotFoundException, IOException {
        DbtLog.logUtils("Bitmap", "开始裁剪");
        //int degree = getBitmapDegree(uri.toString());// 获取图片偏转度
        int degree = 0;
        InputStream input = ac.getContentResolver().openInputStream(uri);
        BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
        onlyBoundsOptions.inJustDecodeBounds = true;
        onlyBoundsOptions.inDither = true;//optional
        onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
        BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
        if (onlyBoundsOptions.outWidth > onlyBoundsOptions.outHeight) {
            degree = 90;
        }
        input.close();
        int originalWidth = onlyBoundsOptions.outWidth;
        int originalHeight = onlyBoundsOptions.outHeight;
        if ((originalWidth == -1) || (originalHeight == -1))
            return null;

        //图片分辨率以480x800为标准
        float hh = 0f;
        float ww = 0f;
        if (degree == 90) {
            hh = 480f;//这里设置高度为640f
            ww = 640f;//这里设置宽度为480f
        } else {// 0
            hh = 640f;//这里设置高度为640f
            ww = 480f;//这里设置宽度为480f
        }

        /*if (degree == 90) {
            hh = 680;//这里设置高度为640f
            ww = 960f;//这里设置宽度为480f
        } else {// 0
            hh = 960f;//这里设置高度为640f
            ww = 680f;//这里设置宽度为480f
        }*/

        //float hh = 1280f;//这里设置高度为640f
        //float ww = 960f;//这里设置宽度为480f
        //缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
        if (originalWidth > originalHeight && originalWidth > ww) {//如果宽度大的话根据宽度固定大小缩放
            be = (int) (originalWidth / ww);
        } else if (originalWidth < originalHeight && originalHeight > hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (originalHeight / hh);
        }
        if (be <= 0)
            be = 1;
        //比例压缩
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = be;//设置缩放比例
        bitmapOptions.inDither = true;//optional
        //bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
        bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_4444;//optional
        input = ac.getContentResolver().openInputStream(uri);
        Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
        //bitmap = rotateBitmapByDegree(bitmap,degree);
        if (degree == 90) {
            bitmap = rotateBitmapByDegree(bitmap, degree);
        }
        // bitmap = zoomImg(bitmap, 680, 960);// 设置bitmap的尺寸
        input.close();

        DbtLog.logUtils("Bitmap", "图片读取成功");
        return compressImage(bitmap);//再进行质量压缩
    }

    // 设置bitmap的尺寸
    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }


    /**
     * 通过uri获取图片并进行压缩
     *
     * @param path
     */
    public static Bitmap getBitmapFormPath2(String path) throws FileNotFoundException, IOException {
        int degree = getBitmapDegree(path);// 获取图片偏转度
        BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
        onlyBoundsOptions.inJustDecodeBounds = true;//不加载图片到内存，仅获得图片宽高
        onlyBoundsOptions.inDither = true;//optional
        onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
        Bitmap bitmap = BitmapFactory.decodeFile(path, onlyBoundsOptions);//此时返回bm为空
        int originalWidth = onlyBoundsOptions.outWidth;
        int originalHeight = onlyBoundsOptions.outHeight;
        if ((originalWidth == -1) || (originalHeight == -1))
            return null;

        //图片分辨率以480x800为标准
        float hh = 640f;//这里设置高度为640f
        float ww = 480f;//这里设置宽度为480f
        //缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
        if (originalWidth > originalHeight && originalWidth > ww) {//如果宽度大的话根据宽度固定大小缩放
            be = (int) (originalWidth / ww);
        } else if (originalWidth < originalHeight && originalHeight > hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (originalHeight / hh);
        }
        if (be <= 0)
            be = 1;
        //比例压缩
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = be;//设置缩放比例
        bitmapOptions.inDither = true;//optional
        bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
        bitmap = BitmapFactory.decodeFile(path, bitmapOptions);
        bitmap = rotateBitmapByDegree(bitmap, degree);

        return compressImage(bitmap);//再进行质量压缩
    }

    /**
     * 获取图片的旋转角度
     *
     * @param path 图片绝对路径
     * @return 图片的旋转角度
     */
    public static int getBitmapDegree(String path) {
        int degree = 0;
        try {
            // 从指定路径下读取图片，并获取其EXIF信息
            ExifInterface exifInterface = new ExifInterface(path);
            // 获取图片的旋转信息
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

    /**
     * 将图片按照指定的角度进行旋转
     *
     * @param bitmap 需要旋转的图片
     * @param degree 指定的旋转角度
     * @return 旋转后的图片
     */
    public static Bitmap rotateBitmapByDegree(Bitmap bitmap, int degree) {
        // 根据旋转角度，生成旋转矩阵
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        // 将原始图片按照旋转矩阵进行旋转，并得到新的图片
        Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
        return newBitmap;
    }


    /**
     * 质量压缩方法
     *
     * @param image
     * @return
     */
    public static Bitmap compressImage(Bitmap image) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法，这里100表示不压缩，把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 25) {  //循环判断如果压缩后图片是否大于100kb,大于继续压缩
            baos.reset();//重置baos即清空baos
            //第一个参数 ：图片格式 ，第二个参数： 图片质量，100为最高，0为最差  ，第三个参数：保存压缩后的数据的流
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%，把压缩后的数据存放到baos中
            // options -= 1;//每次都减少10
            options -= 1;//每次都减少10
            if (options <= 0) {
                options = 0;
                break;
            }
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
        // Bitmap bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length, null);
        DbtLog.logUtils("Bitmap", "放大之前");
        bitmap = zoomImg(bitmap, 680, 960);// 设置bitmap的尺寸
        DbtLog.logUtils("Bitmap", "放大之后");
        return bitmap;
    }




    //----↓ 长按保存照片 ↓----------------------------------------------------------------------------------------------
    public void downPicDate(final String picurl) {
        try {
            AlertDialog.Builder builder = new AlertDialog.Builder(_mActivity);
            builder.setTitle("保存图片");
            builder.setMessage("是否保存该图片？");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // LatteLoader.showLoading(LoginActivity.this, true);
                    download(picurl);
                }
            });
            builder.setNegativeButton("取消", null).create().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 保存图片到手机
    public void download(final String url) {

        new AsyncTask<Void, Integer, File>() {

            @Override
            protected File doInBackground(Void... params) {
                File file = null;
                File destFile = null;
                try {
                    FutureTarget<File> future = Glide
                            .with(_mActivity)
                            .load(url)
                            .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);

                    file = future.get();

                    // 首先保存图片 pictureFolder =  "/storage/emulated/0/Pictures"
                    //File pictureFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile();

                    // 本地文件路径  appDir =  "/storage/emulated/0/Pictures/Beauty"
                    // File appDir = new File(pictureFolder ,"Beauty");
                    // File appDir = new File(EaseImageUtils.getPicPath(_mActivity));
                    File appDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getPath() + "/Camera/");// /storage/emulated/0/DCIM/Camera
                    if (!appDir.exists()) {
                        appDir.mkdirs();
                    }
                    // String fileName = System.currentTimeMillis() + ".jpg";
                    String fileName = url.substring(url.lastIndexOf("/") + 1);
                    // destFile =  "/storage/emulated/0/Pictures/Beauty/1551859435100.jpg"
                    destFile = new File(appDir, fileName);

                    // 复制文件
                    copy(file, destFile);

                    // 最后通知图库更新
                    _mActivity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.fromFile(new File(destFile.getPath()))));


                } catch (Exception e) {
                    // Log.e("EasePagerViewImageActivity", e.toString());
                }
                // return file;
                return destFile;
            }

            @Override
            protected void onPostExecute(File file) {

                // Toast.makeText(BaseActivity.this, "保存成功路径:" + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
                Toast.makeText(_mActivity, "图片已保存到相册", Toast.LENGTH_SHORT).show();
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
            }
        }.execute();
    }

    /**
     * 复制文件
     *
     * @param source 输入文件
     * @param target 输出文件
     */
    public static void copy(File source, File target) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(source);
            fileOutputStream = new FileOutputStream(target);
            byte[] buffer = new byte[1024];
            while (fileInputStream.read(buffer) > 0) {
                fileOutputStream.write(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //----↑ 长按保存照片 ↑----------------------------------------------------------------------------------------------


    public void reStartApp(){
        Intent LaunchIntent = _mActivity.getPackageManager().getLaunchIntentForPackage(Latte.getApplicationContext().getPackageName());
        LaunchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //启动Activity来重启应用程序
        startActivity(LaunchIntent);
        // 关闭所有Activity
        ExitAppUtils.getInstance().exitOnlyLogin();
    }

}
